<?php

	/* postgres sql client class */
    class CMySql
    {
		var $m_hDbms;			//dbms connection handler
		var $m_hQueryResult;	//query result handler 

		var $m_hDName;			//database name


		/* construct object of the class */
		/* return reference to created object */
		static function &GetInstance()
		{
			global $g_hObjects;
			static $m_hApp = NULL;
			
			if(!isset($m_hApp))
			{
				$m_hApp = new CMySql();
				
				$g_hObjects[] = $m_hApp;
				
				$m_hApp->OnInit();
			}
			
			return $m_hApp;
		}
		
		/* initialization routines */
		function OnInit()
		{
			if(!function_exists('mysql_connect'))
			{
				die("ta instalacja PHP nie wsiera MySQL'a");
			}
		}
		
		/* called on script shutdown */
		function OnDestroy()
		{
		}
		
		/* connection initialization */
		function Connect($szHost,$iPort,$szUser,$szPass,$szDbName)
		{
			$szHost = ($iPort == '') ? $szHost : $szHost.':'.$iPort;

			$this->m_hDbms = mysql_connect($szHost,$szUser,$szPass,false);
			

			if( !$this->m_hDbms)
				return false;

			$this->UseDB($szDbName);

			return true;
		}
		
		/* closes connection to dbms */
		function Close()
		{
			if($this->m_hDbms)
				mysql_close($this->m_hDbms);
		}
		
		/* obtain error message */
		function GetErrorMsg()
		{
			return mysql_error();
		}
		
		/* wysyla kwerende do serwera */
		function Query($szQuery)
		{
			$this->m_hQueryResult = mysql_query($szQuery,$this->m_hDbms);

			return ($this->m_hQueryResult == FALSE) ? false : true;
		}
		
		function UseDB($szDbName)
		{
			$szQuery = 'USE '.$szDbName;

			$this->Query($szQuery);
		}
		
		/* set encoding type */
		function SetEncoding($szType)
		{
			$query = "SET NAMES  '$szType' ";
			$this->Query($query);
		}

		
		/* return rows number in result */
		function GetResultCount()
		{
			return  @mysql_num_rows($this->m_hQueryResult);
		}
		
		/* get row from query */
		function GetResultAsRow()
		{
			return @mysql_fetch_assoc($this->m_hQueryResult);
		}
		
		/* get result row as numerated and associated array - both array & association array are supported */
		function GetResultAsArray()
		{
			return @mysql_fetch_array($this->m_hQueryResult,0, MYSQL_BOTH);
		}
		
		/* get whole result from query */
		function GetResult()
		{
			$result = array();
			
			while($entry = $this->GetResultAsRow())
			{
				$result[] = $entry;
			}
			
			return $result;
		}
		
    }

?>